package com.openatc.agent.service.impl;

import com.openatc.agent.service.GenericMsgStrategy;
import com.openatc.agent.utils.RedisTemplateUtil;
import com.openatc.comm.data.MessageData;
import com.openatc.core.model.InnerError;
import com.openatc.core.model.RESTRet;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import static com.openatc.core.common.IErrorEnumImplInner.E_110;

/**
 * @author ：panleilei
 * @description
 * @modified By：
 * @version:
 * @date ：Created in 2024/9/23 15:58
 */
@Service(value = "status/channellamp")
public class StatusChannelLamp extends GenericMsgStrategy {
    private String keyType = "response:status/channellamp:";
    @Autowired
    private RedisTemplateUtil redisTemplateUtil;
    /**
     * 信号机成功应答请求
     * @param requestData
     * @param responseData
     * @return
     */
    @Override
    public RESTRet successGetProcess(MessageData requestData, RESTRet responseData) {
        String agentid = requestData.getAgentid();
        MessageData res = (MessageData) responseData.getData();
        // 保存到redis
        String key = keyType + agentid;
        res.setCreatetime(String.valueOf(System.currentTimeMillis()));
        redisTemplateUtil.setValue(key,gson.toJson(res));
        return super.successGetProcess(requestData,responseData);
    }
    /** 信号机应答失败
     * @param requestData
     * @param responseData
     * @return
     */
    @Override
    public RESTRet failureGetProcess(MessageData requestData, RESTRet responseData) {

        InnerError innerError = (InnerError) responseData.getData();
        // 上次请求未结束/请求超时,从缓存中获取数据
        if (innerError.getErrorCode().equals(E_110.getErrorCode()) ) {

            RESTRet rest = redisTemplateUtil.getCacheData(requestData);
            return rest;
        }

        return super.failureGetProcess(requestData, responseData);
    }
}
